package com.example.translateappjava.Activity;

import static com.example.translateappjava.Activity.MainActivity.increaseCursorWindowSize;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.translateappjava.DataBase.UserDbHelper;
import com.example.translateappjava.R;
import com.example.translateappjava.DataBase.UserInfo;

public class LoginActivity extends AppCompatActivity {


    private EditText et_username;
    private EditText et_password;
    private SharedPreferences mSharedPreferences;
    private CheckBox checkBox;

    private boolean is_login;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        increaseCursorWindowSize();

//        EdgeToEdge.enable(this);
        mSharedPreferences = getSharedPreferences("user",MODE_PRIVATE);


        boolean isLogin = mSharedPreferences.getBoolean("is_login", false);
        String username = mSharedPreferences.getString("username", null);
        String password = mSharedPreferences.getString("password", null);




        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        checkBox = findViewById(R.id.checkbox);
        et_username.setText(username);
        et_password.setText(password);
        if (!et_username.getText().toString().isEmpty()&&!et_password.getText().toString().isEmpty()) {
            if (isLogin) {

                // 如果用户已经登录，直接跳转到主界面
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                UserInfo login = UserDbHelper.getInstance(LoginActivity.this).login(username);
                UserInfo.setsUserInfo(login);
                startActivity(intent);
                finish();
                return; // 防止执行后续的初始化代码
            }

        }




//点击注册
        findViewById(R.id.to_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();

                if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                    Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }else{
                    UserInfo login = UserDbHelper.getInstance(LoginActivity.this).login(username);
                    //                    String name = mSharedPreferences.getString("username", null);
                    //                    String pwd = mSharedPreferences.getString("password", null);
                    if(login!=null){

                        if(username.equals(login.getUsername()) && password.equals(login.getPassword())){
                            //是否勾选了记住密码
                            SharedPreferences.Editor editor = mSharedPreferences.edit();
                            // 登录成功，根据checkBox的状态决定是否保存登录信息
                            if(checkBox.isChecked()){
                                editor.putString("username", username);
                                editor.putString("password", password);
                                editor.apply();
                            } else {
                                editor.clear(); // 如果用户取消勾选记住密码，清除SharedPreferences
                            }
                            UserInfo.setsUserInfo(login);
                            //登陆成功
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(LoginActivity.this, "该账号暂未注册", Toast.LENGTH_SHORT).show();
                    }



                }

            }
        });

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 直接更新SharedPreferences的is_login值
                SharedPreferences.Editor editor = mSharedPreferences.edit();
                editor.putBoolean("is_login", isChecked);
                editor.apply();
            }
        });


    }
    public void clearLoginPreferences() {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove("is_login");
        editor.remove("username");
        editor.remove("password");
        editor.apply();
    }



}

